<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>osapi.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_third_party_closure_goog_osapi_osapi.js.html">osapi.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>/**
<a name="line2"></a> * @license
<a name="line3"></a> * Licensed to the Apache Software Foundation (ASF) under one
<a name="line4"></a> * or more contributor license agreements. See the NOTICE file
<a name="line5"></a> * distributed with this work for additional information
<a name="line6"></a> * regarding copyright ownership. The ASF licenses this file
<a name="line7"></a> * to you under the Apache License, Version 2.0 (the
<a name="line8"></a> * &quot;License&quot;); you may not use this file except in compliance
<a name="line9"></a> * with the License. You may obtain a copy of the License at
<a name="line10"></a> * http://www.apache.org/licenses/LICENSE-2.0
<a name="line11"></a> * Unless required by applicable law or agreed to in writing,
<a name="line12"></a> * software distributed under the License is distributed on an
<a name="line13"></a> * &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
<a name="line14"></a> * KIND, either express or implied. See the License for the
<a name="line15"></a> * specific language governing permissions and limitations under the License.
<a name="line16"></a> */
<a name="line17"></a>
<a name="line18"></a>/**
<a name="line19"></a> * @fileoverview Base OSAPI binding.
<a name="line20"></a> * This file was copied from
<a name="line21"></a> * http://svn.apache.org/repos/asf/shindig/trunk/features/src/main/javascript/features/shindig.container/osapi.js
<a name="line22"></a> * and it&#39;s slightly modified for Closure.
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.osapi&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>// Expose osapi from container side.
<a name="line29"></a>var osapi = osapi || {};
<a name="line30"></a>goog.exportSymbol(&#39;osapi&#39;, osapi);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/** @type {Function} */
<a name="line34"></a>osapi.callback;
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Dispatch a JSON-RPC batch request to services defined in the osapi namespace
<a name="line39"></a> * @param {Array.&lt;Object&gt;} requests an array of rpc requests.
<a name="line40"></a> */
<a name="line41"></a>goog.osapi.handleGadgetRpcMethod = function(requests) {
<a name="line42"></a>  var responses = new Array(requests.length);
<a name="line43"></a>  var callCount = 0;
<a name="line44"></a>  var callback = osapi.callback;
<a name="line45"></a>  var dummy = function(params, apiCallback) {
<a name="line46"></a>    apiCallback({});
<a name="line47"></a>  };
<a name="line48"></a>  for (var i = 0; i &lt; requests.length; i++) {
<a name="line49"></a>    // Don&#39;t allow underscores in any part of the method name as a
<a name="line50"></a>    // convention for restricted methods
<a name="line51"></a>    var current = osapi;
<a name="line52"></a>    if (requests[i][&#39;method&#39;].indexOf(&#39;_&#39;) == -1) {
<a name="line53"></a>      var path = requests[i][&#39;method&#39;].split(&#39;.&#39;);
<a name="line54"></a>      for (var j = 0; j &lt; path.length; j++) {
<a name="line55"></a>        if (current.hasOwnProperty(path[j])) {
<a name="line56"></a>          current = current[path[j]];
<a name="line57"></a>        } else {
<a name="line58"></a>          // No matching api
<a name="line59"></a>          current = dummy;
<a name="line60"></a>          break;
<a name="line61"></a>        }
<a name="line62"></a>      }
<a name="line63"></a>    } else {
<a name="line64"></a>      current = dummy;
<a name="line65"></a>    }
<a name="line66"></a>
<a name="line67"></a>    // Execute the call and latch the rpc callback until all
<a name="line68"></a>    // complete
<a name="line69"></a>    current(requests[i][&#39;params&#39;], function(i) {
<a name="line70"></a>      return function(response) {
<a name="line71"></a>        // Put back in json-rpc format
<a name="line72"></a>        responses[i] = {&#39;id&#39;: requests[i].id, &#39;data&#39;: response};
<a name="line73"></a>        callCount++;
<a name="line74"></a>        if (callCount == requests.length) {
<a name="line75"></a>          callback(responses);
<a name="line76"></a>        }
<a name="line77"></a>      };
<a name="line78"></a>    }(i));
<a name="line79"></a>  }
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Initializes container side osapi binding.
<a name="line85"></a> */
<a name="line86"></a>goog.osapi.init = function() {
<a name="line87"></a>   // Container-side binding for the gadgetsrpctransport used by osapi.
<a name="line88"></a>   // Containers add services to the client-side osapi implementation by
<a name="line89"></a>   // defining them in the osapi namespace
<a name="line90"></a>  if (gadgets &amp;&amp; gadgets.rpc) { // Only define if gadgets rpc exists.
<a name="line91"></a>    // Register the osapi RPC dispatcher.
<a name="line92"></a>    gadgets.rpc.register(&#39;osapi._handleGadgetRpcMethod&#39;,
<a name="line93"></a>        /** @type {!Function} */ (goog.osapi.handleGadgetRpcMethod));
<a name="line94"></a>  }
<a name="line95"></a>};
</pre>


</body>
</html>
